package edu.hawaii.ics414;

import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;

/**
 * Initiates the view for leaving a rating and comment
 * 
 * @author Matthew Mizumoto
 * 
 */

public class CommentView extends Activity {

  /*
   * Called when the activity is first created.
   * 
   * @param savedInstanceState the instance of the layout to be created over.
   */

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.comment_view);

    Intent intent = getIntent();
    final String gameName = intent.getStringExtra("gameName");
    // Toast.makeText(CommentView.this, gameName, Toast.LENGTH_LONG).show();
    final RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
    Button submitButton = (Button) findViewById(R.id.submit_comment_button);
    final EditText commentText = (EditText) findViewById(R.id.comment_box);

    /*
     * Sets the submit button's action to add the comment to the table.
     */
    submitButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Boolean tryComment;
        final String comment = commentText.getText().toString();
        try {
          tryComment =
              new AddReview().execute(CurrentUser.username, gameName, comment,
                  String.valueOf(ratingBar.getRating())).get(5, TimeUnit.SECONDS);

          if (!tryComment) {
            Toast.makeText(CommentView.this, "Comment Added Successfully", Toast.LENGTH_LONG)
                .show();
          }
          else {
            Toast.makeText(CommentView.this, "Unable to Post Comment. Please Try Again",
                Toast.LENGTH_LONG).show();
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }

      }
    });

  }
}